package com.youi.service.impl;

import com.google.common.collect.Lists;
import com.youi.dao.MarketThemeDao;
import com.youi.dao.MarketThemeGoodsDao;
import com.youi.entity.MarketTheme;
import com.youi.entity.MarketThemeGoods;
import com.youi.service.MarketThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MarketThemeServiceImpl extends BaseServiceImpl<MarketTheme> implements MarketThemeService {

    private MarketThemeDao marketThemeDao;

    @Autowired
    private MarketThemeGoodsDao marketThemeGoodsDao;

    @Autowired
    public void setfaultRecordDao(MarketThemeDao marketThemeDao) {
        this.marketThemeDao = marketThemeDao;
        super.setBaseDAO(marketThemeDao);
    }

    @Override
    public List<MarketTheme> listMarketThemeByParam(Map<String, Object> params,
                                                    Map<String, Object> sortParams,
                                                    Map<String, Object> gtParams,
                                                    Map<String, Object> ltParams) {
        return marketThemeDao.getByParamsAll(params, null, sortParams, gtParams, ltParams);
    }

    @Override
    public MarketTheme getMarketThemeById(Long id) {
        //专题信息
        MarketTheme marketTheme = marketThemeDao.get(id);
        if (marketTheme == null) {
            return marketTheme;
        }
        //专题商品
        List<MarketThemeGoods> bannerGoodsList = Lists.newArrayList();
        List<MarketThemeGoods> gdsGoodsList = Lists.newArrayList();
        List<MarketThemeGoods> adsGoodsList = Lists.newArrayList();

        List<MarketThemeGoods> goodsList = marketThemeGoodsDao.listGoodsByThemeId(marketTheme.getId());
        if (!CollectionUtils.isEmpty(goodsList)) {
            Map<Integer, List<MarketThemeGoods>> positionGoodsMap = goodsList.stream().collect(Collectors.groupingBy(MarketThemeGoods::getGoodsPosition));
            if (!CollectionUtils.isEmpty(positionGoodsMap.get(1))) {
                bannerGoodsList = positionGoodsMap.get(1);
            }
            if (!CollectionUtils.isEmpty(positionGoodsMap.get(2))) {
                gdsGoodsList = positionGoodsMap.get(2);
            }
            if (!CollectionUtils.isEmpty(positionGoodsMap.get(3))) {
                adsGoodsList = positionGoodsMap.get(3);
            }
        }
        marketTheme.setHeaders(bannerGoodsList);
        marketTheme.setGoods(gdsGoodsList);
        marketTheme.setAdvertisements(adsGoodsList);

        return marketTheme;
    }
}
